﻿using System;
using System.Collections.Generic;
using DbLib;
using DbLib.models;
using CommonLibCore;
using System.Text;
using System.Linq;
using System.Diagnostics;

namespace BLL
{
    public class UserBll
    {
        public PreUser Login(PreUser user)
        {
            List<Conditioner> condition = new List<Conditioner>();
            condition.Add(new Conditioner { Key = "username", Value = user.Username, Contract = "=" });
            condition.Add(new Conditioner { Key = "password", Value = user.Password, Contract = "=" });
            try
            {
                using (order02Context db = new order02Context())
                {
                    var query = db.PreUser.AsQueryable();
                    string userGroup = null;
                 
                    List<PreUser> list = LinqHelperExtension.Where(query, condition.ToArray()).ToList();
                    if (list.Count > 0)
                    {
                        list[0].Logip = user.Logip;
                        list[0].Logtime = GeneralTools.ConvertDateTimeInt(DateTime.Now);
                        List<string> changeFields = new List<string>();
                        changeFields.Add("logip");                        
                        changeFields.Add("logtime");
                        LinqHelper.Update(list[0], changeFields);
                        
                      
                        return list[0];
                    }

                    return null;
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(DateTime.Now + ex.Message + "\n\r");
                throw ex;
            }
        }
    }
}
